package com.example.demo10;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {
    private  ForceTogo forceTogo;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMange.add(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        IntentFilter intentFilter = new IntentFilter("Force_to_go");
        forceTogo=new ForceTogo();
        registerReceiver(forceTogo,intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (forceTogo!=null){
            unregisterReceiver(forceTogo);
        }
    }

    /**
     * 异地登录广播接收器
     */
    class ForceTogo extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setMessage("你的账号在另一台设备登录,请重新登录");
            builder.setCancelable(false);
            builder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityMange.finish();
                    Intent intent1=new Intent(context,activity_login.class);
                    startActivity(intent1);
                }
            }).show();
        }
    }
}
